package com.wenda.homeschooedu.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.wenda.homeschooedu.biz.ClassesManager;

public class ClassesServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter out = null;
		String result = "";

		if (req.getParameter("format") != null) {
			if (req.getParameter("format").equals("json")) {
				if (req.getParameter("type").equals("get")) {
					resp.setContentType("text/html;charset=utf-8");
					out = resp.getWriter();
					Gson gson = new Gson();
					try {
						int class_id = Integer.parseInt(req
								.getParameter("class_id"));
						result = gson.toJson(new ClassesManager()
								.getClassById(class_id));
					} catch (Exception e) {
						result = gson.toJson(new ClassesManager()
								.getAllClasses());
					}
					out.print(result);
					out.flush();
					out.close();
					
					
				}
			}
		}

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}

}
